var MAX_SEARCH_TIME = 2000; //搜索控件时超时时间 http://doc.autoxjs.com/#/widgetsBasedAutomation?id=uiselectorfindonetimeout

var utils = {
  sleep_and_log(duration) {
    var interval = 5;
    while (duration > interval) {
      sleep(interval * 1000);
      duration = duration - interval;
      console.log("还剩:" + duration + "s");
    }
    if (duration > 0) {
      sleep(duration * 1000);
      console.log("sleep_and_log: duration ", duration, "s");
    }
  },
  click_text(content) {
    if (this.exists_text(content)) {
      var uiText = text(content).findOne(MAX_SEARCH_TIME);
      var bounds = uiText.bounds();
      click(bounds.centerX(), bounds.centerY());
      return uiText;
    } else {
      return false;
    }
  },
  click_text_starts_with(content) {
    if (this.exists_text_starts_with(content)) {
      var uiText = textStartsWith(content).findOne(MAX_SEARCH_TIME);
      var bounds = uiText.bounds();
      click(bounds.centerX(), bounds.centerY());
      return uiText;
    } else {
      return false;
    }
  },
  exists_text(content) {
    if (text(content).exists()) {
      return text(content).findOne(MAX_SEARCH_TIME);
    } else {
      return false;
    }
  },
  exists_text_starts_with(content) {
    if (textStartsWith(content).exists()) {
      return textStartsWith(content).findOne(MAX_SEARCH_TIME);
    } else {
      return false;
    }
  },
  get_center_point(x1, y1, x2, y2) {
    return {
      x: x1 + (x2 - x1) / 2,
      y: y1 + (y2 - y1) / 2,
    };
  },
  click_point(x1, y1, x2, y2) {
    var point = this.get_center_point(x1, y1, x2, y2);
    return click(point.x, point.y);
  },
  click_bounds(bounds) {
    click(bounds.centerX(), bounds.centerY());
  },
  click_ui(ui_item) {
    while (true) {
      if (ui_item && ui_item.clickable()) {
        ui_item.click();
        return true;
      } else if (ui_item) {
        ui_item = ui_item.parent();
      } else {
        return false;
      }
    }
  },
  click_x_y(x, y) {
    click(x, y);
  },
  swipe_up(duration, distance) {
    while (duration > 3) {
      swipe(
        device.width / 2,
        device.height / 2,
        device.width / 2,
        device.height / 2 - distance,
        500 + random(0, 10)
      );
      var interval = 1 + random(0, 2);
      sleep(interval * 1000);
      duration = duration - interval;
    }
    if (duration > 0) {
      sleep(duration * 1000);
    }
    utils.toast("swipe_up end");
  },
  toast(content) {
    console.log(content);
    toast(content);
    sleep(6 * 1000);
  },
  sleep(duration) {
    var duration = duration / 1000;
    while (duration > 4) {
      sleep(4 * 1000);
      duration = duration - 4;
    }
    if (duration > 0) {
      sleep(duration * 1000);
    }
  },
  get_gold(content) {
    var reg_exp = /(\d+)/g;
    var gold_match = content.match(reg_exp);
    var gold = parseInt(gold_match ? gold_match[0] : 0);
    return gold;
  },
  get_size_point(points) {
    var length = points.length;
    var size_point = [];
    var rate_x = device.width / 720;
    var rate_y = device.height / 1650;
    for (var i = 0; i < length; i++) {
      var x_y = points[i];
      var rate = i % 2 == 0 ? rate_x : rate_y;
      size_point.push(parseInt(x_y * rate));
    }
    return size_point;
  },
};

if (!requestScreenCapture()) {
  toastLog("请求截图权限失败");
  exit();
}

function capture_screen_then_log_text() {
  var capture_img = captureScreen();
  var capture_img_orc_result = gmlkit.ocr(capture_img, "zh").toArray(3);
  for (var i = 0; i < capture_img_orc_result.length; i++) {
    var item = capture_img_orc_result[i];
    console.log(item.text);
  }
}

var char_match_map = {
  搜: ["嫂", "樓", "接", "楼"],
  赚: ["廉", "嫌", "賺", "兼"],
  看: ["着", "春"],
  文: ["女"],
  章: ["拿"],
  钱: ["线"],
  觉: ["党"],
  户: ["戶"],
  索: ["素"],
  开: ["升", "研"],
  金: ["全"],
  积: ["枳"],
  币: ["市"],
  现: ["現"],
  领: ["領"],
  读: ["渎"],
  额: ["額"],
  取: ["驭"],
  视: ["规"],
};

function capture_screen_then_find_text(text) {
  var capture_img = captureScreen();
  var reg_exp_string = text
    .split("")
    .map(function (item) {
      var match = char_match_map[item];
      var piece = match
        ? "[" + match.concat(item).join("") + "]+"
        : "[" + item + "]+";
      return piece + `\\s*`;
    })
    .join("");
  var reg_exp = new RegExp(reg_exp_string, "gi");
  console.log("capture_screen_then_find_text", reg_exp.toString());
  if (!capture_img) {
    toastLog("截图失败");
  }
  var capture_img_orc_result = gmlkit.ocr(capture_img, "zh").toArray(3);

  for (var i = 0; i < capture_img_orc_result.length; i++) {
    var item = capture_img_orc_result[i];
    if (reg_exp.test(item.text)) {
      return item;
    }
  }
  // for (var i = 0; i < capture_img_orc_result.length; i++) {
  //   var item = capture_img_orc_result[i];
  //   console.log(item.text);
  // }
}

function find_text(text) {
  var max_swipe_times = 10;
  while (max_swipe_times > 0) {
    var result = capture_screen_then_find_text(text);
    if (result) {
      return result;
    }
    swipe(
      device.width / 2,
      device.height / 2,
      device.width / 2,
      device.height / 2 - 200,
      500
    );
    max_swipe_times -= 1;
  }
}

function test() {
  var result = find_text("随机搜赚钱");
  if (result) {
    var bounds = result.bounds;

    for (var i = 0; i < 30; i++) {
      console.log(bounds.centerX(), bounds.centerY());
      utils.click_bounds(bounds);
      var search_swipe_times = 7 + random(0, 2);
      while (search_swipe_times > 0) {
        swipe(
          device.width / 2,
          device.height / 2,
          device.width / 2,
          device.height / 2 - 200,
          500
        );
        sleep(3 * 1000);
        search_swipe_times = search_swipe_times - 1;
      }
      back();
      if (utils.click_text_starts_with("残忍离开")) {
      }
      sleep(2 * 1000);
    }
  } else {
    console.log("没找到【随机搜赚钱】");
  }
}

// test();
var result = capture_screen_then_find_text("开心收下");

if (result) {
  utils.click_bounds(result.bounds);
}
